const path = require('path');
const express = require('express');
const favicon = require('serve-favicon');
const ejs = require('ejs');
const ejsLayout = require('express-ejs-layouts');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const compression = require('compression');

// create express app
const app = express();

// Base setting
app.disable('x-powered-by'); // hide server symbol
app.use(compression());      // gzip
/*
 * http-proxy 和 body-parser 冲突，用 filterProxy 方法处理，如下
 *
* */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'views')));

/*
* set ejs as view engine
* */
app.use(ejsLayout);
app.set("layout extractScripts", true);
app.set('layout','shared/layout');
app.set('view engine', 'ejs');
app.engine('.ejs', ejs.__express);

/*
* public file folder set 2 hours cache
* */
app.use('/public', express.static('public', {
    'maxAge': process.env.NODE_ENV === 'production' ? '2h' : '0'
}));

/*
* static file folder set 30 days cache
* because it is not usually change
* */
app.use('/static', express.static('static', {
    'maxAge': '30d',
    'ETag': true,
    'lastModified': true
}));
app.use(favicon(path.join(__dirname,'static/images/favicon.png')));

/*
* * * * * * * * * * * * * * * * *  interceptor start  * * * * * * * * * * * * * * * *
* */

/*
* middle ware
* */

// check current env
const checkCurrentEnv = require('./middle_ware/check_current_env');

// check user isLogin
const checkLogin = require('./middle_ware/check_login');

// check user status
const checkUserStatus = require('./middle_ware/check_user_status');

app.use(checkCurrentEnv);
app.use(checkLogin);

/*
* * * * * * * * * * * * * * * * *  interceptor ending  * * * * * * * * * * * * * * * *
* */


/*
* * * * * * * * * * * * * * * * *  routes start  * * * * * * * * * * * * * * * *
* */
/*
* set all routes
* control routes load and change
* */
const page_index_route = require('./routes/route'),              // index
      page_my_route = require('./routes/route_my');              // my
app.use('/', page_index_route);
app.use('/my', checkUserStatus, page_my_route);


/*
* * * * * * * * * * * * * * * * *  routes ending  * * * * * * * * * * * * * * * *
* */


/*
*  * * * * * * *  errors handler start  * * * * * * *
* */

//catch 404 and forward to error handler
app.use(function (req, res, next) {
    if(req.url === '/favicon.ico') return;
    const err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    switch (err.status){
        case 404:
            res.render('shared/404', {
                title: '404 页面不存在',
                layout: 'shared/404'
            });
            break;
        default:
            res.render('shared/500', {
                message: err.message,
                title: '500 服务器错误',
                layout: 'shared/500'
            });
    }
});

/*
*  * * * * * * *  errors handler ending  * * * * * * *
* */

module.exports = app;
